<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Orders extends CI_Controller {
    public function index()
    {
        $this->load->model('defaults');
        
        if (!$this->defaults->ordersAllowed()){
            $this->session->set_flashdata('message',"Ordering service is currently disabled.");
            redirect();
        }
        
        $this->load->model('mealtimes');
        $this->load->model('user_orders');
        
        if (!$this->mealtimes->isOrderable()){
            $this->session->set_flashdata('message',"Orders have been disabled for today.");
            redirect();
        }

        $year = date("Y");
        $month = date("n");
        $day = date("j");

        $mealtime = $this->mealtimes->getMealID($year, $month, $day);
        if (!isset($mealtime)){
            $this->mealtimes->create($year,$month,$day);
            $mealtime = $this->mealtimes->getMealID($year, $month, $day);
        }
        $orders = $this->user_orders->getDay($mealtime->mealtime_id);
        $cutoff = strtotime($mealtime->mealtime_cutoff);
        
        if ($orders->num_rows() == 0){
            $this->session->set_flashdata('message',"There are no orders at the moment.");
            redirect();
        }
        
        if (time() >= $cutoff){
            $data['cutoff'] = "closed as of ".unix_to_time($cutoff);
        }
        else{
            $data['cutoff'] = "open until ".unix_to_time($cutoff);
        }


        $data['title'] = "Tonight's Orders";
        $data['data'] = array ();

        $data['css'] = array("table.css");

        $data['date'] = date("D jS \of F, Y, g:i a");
        $data['orders'] = $orders;


        $data['view'] = "current_orders/view_orders";
        $this->load->view('view_template',$data);
    }
    
    public function printOrders($mealtime_id = -1){
        $this->load->model('mealtimes');
        $this->load->model('user_orders');
        $this->load->library('fpdf', array('P','mm','A4'));
        $this->load->helper('text');
        
        if ($mealtime_id < 0){
            $year = date("Y");
            $month = date("n");
            $day = date("j");
            
            $mealtime = $this->mealtimes->getMealID($year, $month, $day);
            if (!isset($mealtime)){
                $this->mealtimes->create($year,$month,$day);
                $mealtime = $this->mealtimes->getMealID($year, $month, $day);
            }
        }
                
        $orders = $this->user_orders->getDay($mealtime->mealtime_id);
        $orderAmount = $orders->num_rows();
        if ($orderAmount == 0){
            $this->fpdf->AddPage();
            $this->fpdf->Output();
            return;
        }
        
        $orderArray = $orders->result();
        $orderCount = 0;
        
        for ($pages = 0; $pages < ($orderAmount/20); $pages++){
            $this->fpdf->AddPage();
            $this->fpdf->SetMargins(0,0);
            $xMargin = 8;
            $yMargin = 12.7;
            
            for ($i = 0;  $i < 20; $i++){//Fit 20 on a page
                
                $this->fpdf->SetDrawColor(0,0,0);
                
                if ($orderCount == $orderAmount){
                    break;
                }
                
                $result = $orderArray[$orderCount];
                $orderCount++;
                
                if ($i % 2 == 0){//Left side
                    $x = $xMargin;
                }
                else{//right side
                    $x = $xMargin + 100;
                }
                $y = ($yMargin+(intval($i/2)*27.9));
                
                $name = ellipsize('Name: '.$result->user_title,25,1,"...");
                $meal = ellipsize($result->meal_title,20,1,"...");
                $comment = ellipsize($result->user_order_comment,160,1,"...");

                $commentArr = str_split($comment, 60);

                //The formatting of the output
                $this->fpdf->SetFont('Arial','B',10);
                $this->fpdf->text($x,$y,$name);
                $this->fpdf->SetFont('Arial','BI',12);
                $mealWidth = $this->fpdf->GetStringWidth($meal);
                $this->fpdf->text($x+90-$mealWidth,$y+5,$meal);
                $this->fpdf->SetFont('Arial','B',10);
                $this->fpdf->text($x, $y+8,'Comment:');

                $this->fpdf->SetFont('Arial','I',10);
                $yComment = $y + 12;
                foreach($commentArr as $comm){
                    $this->fpdf->text($x,$yComment,$comm);
                    $yComment += 4;
                }
                $this->fpdf->SetDrawColor(180,180,180);
                $this->fpdf->rect($x-1,$y-5,95,26);
            }
        }
        
        $this->fpdf->Output();
    }
}